$(function(){
	typeList();
})


function typeList(){
	var arr = {
		hf_s_id: 'WD11',
		hf_srv_id: 'Cv_jcus_shoplist',
		hs_action_id: 'get_list'
	}
	getAjaxRequest(arr,function(result){
		sessionStorage.setItem("typeId",result[0].bs_id);
		var htmllist = '';
		var len = result.length;
		if ( len > 0 ) {
			productList(result[0].bs_id);
			for ( var i = 0; i<len; i++) {
				htmllist += '<option value="'+result[i].bs_id+'">'+result[i].bs_value+'</option>';
			}
			$('#select-classify').html(htmllist);
		}else {
			$('#select-classify').html();
		}
	})
}
var productIds = [];
var productQtys = [];
function productList(typeId){
	productIds = [];
	productQtys = [];
	var arr = {
		hf_s_id: 'WD11',
		hf_srv_id: 'Cv_jcus_shoplist',
		hs_action_id: 'get_list_w_detail',
		hs_list_id:typeId
	}
	getAjaxRequest(arr,function(results){
		console.log(results)
		var htmllist = '';
		var len = results.bvc_list_pdt.length;
		var result = results.bvc_list_pdt;
		if( len > 0 ){
			for ( var i = 0; i<len; i++) {
				productIds[i] = result[i].bj_pdt_id ;
				productQtys[i] = result[i].bj_qty ;

				//var isOffer= result[i].bs_is_offer == "Y" ? '<span><em>offer</em></span>' : '';
				htmllist += '<div class="item" data-id="'+result[i].bj_pdt_id+'" data-qty="'+result[i].bj_qty+'">'+
							'<div class="proImg" >'+
							'<img src="'+MPJ.apiBase+'/'+result[i].bs_simg_file+'"/>'+/*isOffer+*/

							'</div>'+
							'<div class="mui-media-body">'+
							'<div class="pro-name">'+result[i].bs_brand+'</div>'+
							'<div class="pro-f-name">'+result[i].bs_volume+'</div>'+
							'<div class="price">$'+result[i].bj_rsp+'</div>'+
							'<div class="price-num">'+
							'<span>Qty:<em>1</em></span>'+
							'<a href="javascript:void(0)" class="addCar mui-btn orderToCart"  data-id="'+result[i].bj_pdt_id+'" data-qty="'+result[i].bj_qty+'">Add<img src="'+MPJ.base+'/resources/images/icon/main-shopcar.png"></a>'+
							'</div>'+
							'</div>'+
							'<div class="deleteBtn" data-id="'+result[i].bj_pdt_id+'" data-name="'+results.bs_list_name+'" data-list="'+results.bj_list_id+'" data-qty="'+result[i].bj_qty+'">'+
							'<img src="'+MPJ.base+'/resources/images/icon/delete-icon.png" />'+
							'</div>'+
							'</div>';
			}
			$('.listBox1').html(htmllist);
			delItem();
			add();
			detail();
		}else {
			$('.listBox1').html(no_result);
		}
	})
}

//购物清单下拉框选择事件
function change(){
	var typeId = $('#select-classify  option:selected').val();
	sessionStorage.setItem("typeId",typeId)
	productList(typeId);
}

function create(typeName){
	var arr = {
		hf_s_id: 'WD11',
		hf_srv_id: 'Cv_jcus_shoplist',
		hs_action_id: 'create_list',
		hs_list_name:typeName
	}
	getAjaxRequest(arr,function(result){
		typeList();
		mui.toast("创建成功");
	})
}

function delAll(){
	var typeId = $('#select-classify  option:selected').val();
	var arr = {
		hf_s_id: 'WD11',
		hf_srv_id: 'Cv_jcus_shoplist',
		hs_action_id: 'delete_list',
		hs_list_id:typeId
	}
	getAjaxRequest(arr,function(result){
		typeList();
		mui.toast("删除成功");
	})
}

/*//删除单个item
$('.listBox .item').each(function(index){
	var $this=$(this);
	var $btn=$this.find('.deleteBtn')
	$btn.on('tap',function(){
		$this.remove();
		if($('.listBox .item').length==0){
			$('.listBox').html(no_result);
		}
	})
})*/

//删除整个清单
$('.delList').on('tap',function(){
	dialog('','Are you sure want to delete “Tomorrow BBQ list”from your shopping lists',3);
		var $dialog = $('#dialog');
		$dialog.show();
	    $dialog.find('.comfirmBtn').one('click', function () {
			delAll();
	        $dialog.hide();
	        $dialog.remove();
	    	$('.listBox .item').remove();
	    	$('.listBox').html(no_result);
	    	mui('#offCanvasContentScroll').scroll().scrollTo(0,0,100);
	    });		
})


//创建一个清单
$('#createList').on('tap',function(){
	dialog('Shopping List Name','<input type="text" placeholder="Please enter here" id="newListName">',1);
	var $dialog = $('#dialog');
	$dialog.show();
    
    $dialog.find('.comfirmBtn').one('click', function () {
		var name = $('#newListName').val();
		create(name);
        $dialog.hide();
        $dialog.remove();
    });
})
//添加单个商品到购物车
function addCart(id,qty) {
		var arr = {
	 hf_s_id: "WD11",
	 hf_srv_id: "Av_jcart",
	 hs_action_id: "add_all_to_cart",
	 hvs_add_pdt_id:id,
	 hvs_add_qty: qty
	 };
	getAjaxRequest(arr, function (result) {
		if (result.bs_result == BS_RESULT.OK) {
			if (result.bs_is_login == BS_IS_LOGIN.Y) {
				dialog('Message', 'Add shopping cart success!', 1);
				var $dialog = $('#dialog');
				$dialog.show();
				$dialog.find('.comfirmBtn').one('click', function() {
					getCartSumCount();
					$dialog.hide();
					$dialog.remove();
				})
			} else {
				location.href = MPJ.base + "/pages/login.jsp";
			}
		}

	});
}
//添加整个购物清单内的商品到购物车
/*function addCarts() {
	var lastUrl = '?hf_s_id=WD11&hf_srv_id=Av_jcart&hs_action_id=add_all_to_cart';
	for(var i = 0; i<productIds.length; i++){
		lastUrl += '&hvs_add_pdt_id='+productIds[i]+'&hvs_add_qty='+productQtys[i];
	}
	var url = MPJ.apiUrl + lastUrl;
	console.log(url)
			getAjaxRequest({}, function (result) {

				if (result.bs_result == BS_RESULT.OK) {
					if (result.bs_is_login == BS_IS_LOGIN.Y) {
						location.href = MPJ.base + "/pages/cart/list.jsp";
					} else {
						location.href = MPJ.base + "/pages/login.jsp";
					}
				}

		},url);
}

//添加购物清单内的所有商品到购物车
$('.addCarts').on('tap',function(){
	addCarts();
})*/



function delItem(){
	$('.item').each(function(){
		var $this = $(this).find('.deleteBtn');
		var id = $(this).find('.deleteBtn').data("id");
		var typeName = $(this).find('.deleteBtn').data("name");
		var listId = $(this).find('.deleteBtn').data("list");
		var qty = $(this).find('.deleteBtn').data('qty');

		$this.on('tap',function(){
			productIds = deleteByValue(productIds,id,productQtys);

			var lastUrl = '?hf_s_id=WD11&hf_srv_id=Cv_jcus_shoplist&hs_action_id=update_list';
			lastUrl += '&hs_list_id='+listId+'&hs_list_name='+typeName;
			for( var i = 0; i<productIds.length; i++){
				lastUrl += '&hvs_pdt_id='+productIds[i]+'&hvs_pdt_qty='+productQtys[i];
			}
			var url = MPJ.apiUrl + lastUrl;

			getAjaxRequest({},function(result){
					console.log(sessionStorage.getItem("typeId"));
					productList(sessionStorage.getItem("typeId"));
			},
			url);
		})

	})
}

function detail(){
	$('.horizontal-list .item').each(function() {
		console.log(4)
		var $this = $(this);
		$this.on('tap', '.proImg', function () {
			console.log(5)
			var pdt_id = $this.data('id');
			var item_no = $this.data('qty');
			var product_info = {};

			product_info.pdt_id = pdt_id;
			product_info.item_no = item_no;
			sessionStorage.setItem('product_info', JSON.stringify(product_info));
			location.href = MPJ.base + "/pages/product/product-detail.jsp";
		})
	})
}